package com.hbnu.contorller;

import com.hbnu.pojo.User;
import org.omg.PortableInterceptor.ServerRequestInfo;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
//@RestController    //被RestController注解修饰的控制器中所有的方法返回的是数据而不是视图，RestController注解相当于Controller注解和ResponseBody注解的组合
public class HomeController {

    @RequestMapping("/home")
    public String homePage(){
        return "home";  //返回的是页面的名字
    }

    @RequestMapping("/param1")
    @ResponseBody   //表示返回的是数据而不是视图
    public void param1(String name,Integer age){
        System.out.println("接受到的数据：" + name + "--->" + age);
    }

    @RequestMapping("/param2")
    @ResponseBody
    public void param2(User user){
        System.out.println("接受到的表单数据:" + user);
    }

    @RequestMapping("/param3")
    @ResponseBody
    public void param3(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("接收到的时间："+date);
        System.out.println("格式化后的时间：" + simpleDateFormat.format(date));
    }

    //自定义时间格式
    @InitBinder  //初始数据绑定
    public void initBinder(ServletRequestDataBinder binder){
        binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }

    @RequestMapping("/forward")
    public String testForward(){
        System.out.println("我处理不了，找人帮你处理。。。。");
        return "forward:home";
    }

    @RequestMapping("/redirect")
    public String testRedirect(){
        System.out.println("我不想处理，你去找别人。。。");
        return "redirect:home";
    }

    @RequestMapping("/character")
    @ResponseBody
    public String testCharacter(){
        return "我是中文数据，你能看懂吗？";
    }

    @RequestMapping("/userInfo")
    public String userInfo(Model model){
        model.addAttribute("name","Jack");
        model.addAttribute("age",20);
        return "home";
    }

    public List<User> findAllUser(){
        List<User> userList = new ArrayList<>();
        userList.add(new User(1,"xiaochai","湖北恩施",9000000.00));
        userList.add(new User(2,"xiaocha","湖北恩施",10000000.00));
        userList.add(new User(3,"xiaoch","湖北恩施",30000000.00));
        userList.add(new User(4,"xiaoc","湖北恩施",40000000.00));
        userList.add(new User(2,"xiao","湖北恩施",70000000.00));
        return userList;
    }
}

